<?php
/**
 * Combo Bulletin Board
 *
 * =======================================================
 *
 * @package     Combo Bulletin Board
 * @author      Denis Wróbel
 * @license     http://www.gnu.org/licenses/gpl-3.0.html
 *
 * =======================================================
 *
 * $Date$
 * $Revision$
 * $Author$
 * $Test$
 * 
 */

if( !defined( 'IN_COMBO' ) || IN_COMBO == false )
{
	die( 'Go away...!' );
}

class ComboCore
{
	/**
	 * Board configuration
	 * 
	 * @var array
	 */
	public $conf					= array();
	
	/**
	 * Language strings
	 * 
	 * @var array
	 */
	public $lang					= array();
	
	/**
	 * Client ip
	 * 
	 * @var string
	 */
	private $client_ip				= '';
	
	/**
	 * Client agent
	 * 
	 * @var string
	 */
	private $client_agent			= '';
	
	/**
	 * Cache class object
	 * 
	 * @var object
	 */
	public $cache					= NULL;
	
	/**
	 * Database class object
	 * 
	 * @var object
	 */
	public $db						= NULL;


	/**
	 * Constructor, initialise most of magic
	 */
	public function __construct()
	{
		//Set client data (ip & agent)
		$this->client_agent = $_SERVER['HTTP_USER_AGENT'];
		$this->client_ip    = $_SERVER['REMOTE_ADDR']; 
		
		
		###########################################
		### Cache class initialization
		###########################################
		require_once( CLASSES . 'cache.Class.php' );
		$this->cache = new Cache( $this );
		###########################################
		
		###########################################
		### Database class initialization
		###########################################
		require_once( KERNEL . 'Config.php' );
		require_once( CLASSES . 'db.Class.php' );
		$this->db = new Datbase( $this, $config );
		
		
		###########################################
		### Board configuration
		###########################################
		$settings_cache = $this->cache->readCache( 'settings' ); //Load cache with settings
		if( $this->cache->getLoadMessage() == CACHE_NOTEXISTS ) //Check if cache is exists
		{
			$conf 			= array();
			$settings_query = $this->db->query( 'SELECT *
												 FROM ' . DB_PREFIX . 'settings' ); //Get settings from db
			
			if( !$settings_query )
			{
				trigger_error( 'Can\'t get board configuration from database.' );
			}
			
			while( $settings_fetch = $this->db->fetchArray( $settings_query ) )
			{
				$conf[ $settings_fetch[0] ] = $settings_fetch[1];
			}
			
			$this->db->freeMemory( $settings_query );
			unset( $settings_fetch );
			$this->conf = $conf;
			
			$this->cache->saveCache( 'settings', $conf, 600 ); //Save cache for 600seconds
		}
		elseif( $this->cache->getLoadMessage() == CACHE_TIMEOUT ) //Check if cache is timeout
		{
			$this->cache->deleteCache( 'settings' ); //Delete old cache
			
			$this->conf = $settings_cache;
		}
		else
		{
			$this->conf = $settings_cache;
		}
		
		unset( $settings_cache );
		############################################
		
		$this->addSetting( 'board_template', 'clear' );
	}
	
	/**
	 * Change board setting
	 * 
	 * @param string $id    Setting id
	 * @param string $value Setting value
	 */
	public function updateSetting( $id, $value )
	{
		if( isset( $this->conf[ $id ] ) )
		{
			
			$this->conf[ $id ] = $value;
			
			$this->db->update( array( 'setting_value' => $this->db->escape( $value ) ), 'settings', 'setting_name = \'' . $id . '\''  );
			
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Add new board setting
	 * 
	 * @param string $id    Setting id
	 * @param string $value Setting value
	 */
	public function addSetting( $id, $value )
	{
		if( !isset( $this->conf[ $id ] ) )
		{
			$this->conf[ $id ] = $value;
			
			$this->db->insert( array( 'setting_name' => $this->db->escape( $id ), 'setting_value' => $this->db->escape( $value ) ), 'settings'  );
			
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Returns client ip
	 * 
	 * @return string
	 */
	public function getClientIp()
	{
		return $this->client_ip;
	}
	
	/**
	 * Returns client agent
	 * 
	 * @return string
	 */
	public function getClientAgent()
	{
		return $this->client_agent;
	}
	
	/**
	 * Get value from POST
	 * 
	 * @param string $key
	 */
	public function _POST( $key )
	{
		if( isset( $_POST[ $this->arrToStr( $key ) ] ) )
		{
			return trim( $_POST[ $this->arrToStr( $key ) ] );
		}
		else 
		{
			return NULL;
		}
	}
	
	/**
	 * Get value from GET
	 * 
	 * @param string $key
	 */
	public function _GET( $key )
	{
		if( isset( $_GET[ $this->arrToStr( $key ) ] ) )
		{
			return trim( $_GET[ $this->arrToStr( $key ) ] );
		}
		else 
		{
			return NULL;
		}
	}
	
	public function arrToStr( $arr )
	{
		if( is_array( $arr ) )
		{
			$str = '';
			
			while( is_array( $arr ) )
			{
				$str = $arr[0];
			}
			
			return $str;
		}
		else
		{
			return $arr;
		}
	}
	
	/**
	 * Trigger error page
	 * 
	 */
	public function triggerError( $error_title, $reasons )
	{
		$reasons_html = '';
		
		if( is_array( $reasons ) )
		{
			foreach( $reasons AS $reason )
			{
				$reasons_html .= '<li>' . $reason . '</li>';
			}
		}
		else
		{
			$reasons_html = $reasons;
		}
		
		$error = '<html><head><title>Script Error - ' . SCRIPT_NAME . ' ' . VERSION_HUMAN . '</title><style type="text/css">html, body{width: 700px;margin: 0px auto;font-family: Verdana;}div.border{border: 1px #dddddd solid;padding: 1px;}div.header{font-weight: bold;background-color: #8b1e19;width: 688px;color: white;padding: 4px;}div.liner{height: 3px;width: 100%;background-color: #deaeac;}div.title{font-family: Arial;font-size: 13px;font-weight: normal;padding: 14px;}div.bar{border: 2px #8b1e19 solid;border-left: 0px;border-right: 0px;width: 692px;font-family: Arial;font-size: 13px;background-color: #f8ecec;padding: 2px;}div.message{list-style-type: square;padding: 7px;font-size: 12px;}</style></head><body><div style="width: 1px;height: 150px;"></div><div class="border"><div class="header">Error was occured</div><div class="liner"></div><div class="title">' . $error_title . '</div><div class="bar">Possible causes of error</div><div class="message">' . $reasons_html . '</div></div></body></html>';
		
		echo $error;
		die();
	}
}

?>